package com.gyd.marte;

import java.util.ArrayList;

import com.gyd.dto.Empresa;
import com.gyd.pojos.promociones;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Welcome extends Activity {
	ListView sugerencia;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bienvenida);

        getEmpresas();
    	Button registro= (Button)this.findViewById(R.id.search);
        registro.setOnClickListener(new OnClickListener(){
        	public void onClick(View arg0){
        		Log.i("INFOTAG", "opening a new view with the page of searchs!! ");
        		//getPromos(-1);
        		lanzarBusqueda(arg0);
        	}
        	                               
        	
        });
        Button consultar= (Button)this.findViewById(R.id.promos);
        consultar.setVisibility(consultar.INVISIBLE);
        consultar.setOnClickListener(new OnClickListener(){
        	public void onClick(View arg0){
        		Log.i("INFOTAG", "opening a new view with the page of searchs!! ");
        		getEmpresas();
        		//lanzarBusqueda(arg0);
        	}
        	                               
        	
        });
        
        ListView lv = (ListView)findViewById(R.id.sugerencia);
        lv.setOnItemClickListener(new OnItemClickListener(){
        	public void onItemClick(AdapterView<?> parent, View view,int position, long id){
        		getPromos(position+1);
        	}
        	
        });

    }

    private void getEmpresas(){
        ListView lv = (ListView)findViewById(R.id.sugerencia);
        
        ArrayList<Empresa> itemsPromo = obtenerItems();
        
        ItemEmpresaAdapter adapter = new ItemEmpresaAdapter(this, itemsPromo);
             
        lv.setAdapter(adapter);    	
    }
    
    private void getPromos(int idRestau){
        ListView lv = (ListView)findViewById(R.id.sugerencia);
        Button consultar= (Button)this.findViewById(R.id.promos);
        consultar.setVisibility(consultar.VISIBLE);
        ArrayList<promociones> itemsPromo = obtenerPromos(idRestau);
        
        ItemPromoAdapter adapter = new ItemPromoAdapter(this, itemsPromo);
             
        lv.setAdapter(adapter);    	
    }
    
    private ArrayList<promociones> obtenerPromos(int idRestau) {
        ArrayList<promociones> items1 = new ArrayList<promociones>();
        ArrayList<promociones> items = new ArrayList<promociones>();
             
        items1.add(new promociones(1, 1,"Promo Especial", "Q25", "drawable/bigmac"));
        items1.add(new promociones(2, 1,"Mcrib is back", "Q37", "drawable/mcrib"));
        items1.add(new promociones(3, 2,"3 pizzas", "Q109", "drawable/pizzas"));
        if(idRestau>0){
        for(promociones prom: items1){
        	if(prom.getIdRestaurante()==idRestau){
        		items.add(prom);
        	}
        }
        return items;
        }
        else
        	return items1;
      }
    
  private ArrayList<Empresa> obtenerItems() {
    ArrayList<Empresa> items = new ArrayList<Empresa>();
         
    items.add(new Empresa(1, "Mc", "zona 10", "drawable/mc"));
    items.add(new Empresa(2, "PizzaHut", "zona 15", "drawable/hut"));
    items.add(new Empresa(3, "BurgerKing", "zona 13", "drawable/burger"));
         
    return items;
  }
  
    public void lanzarBusqueda(View view) {
        //Intent i = new Intent(this, InitialActivity.class );
    	Intent i = new Intent(this,InitialActivity.class );
        startActivity(i);
  }
    
    public void cerrar(View view) {
    	finish();
    }
}
